<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <ImpExcel @success="excelSuccessFn" dateFormat="YYYY-MM-DD">
          <a-button class="m-3" :loading="loading"> 导入Excel </a-button>
        </ImpExcel>

        <a-button @click="aoaToExcel"> 导出模版 </a-button>

        <a-button type="primary" @click="handleCreate"> 新增 </a-button>

      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleEdit.bind(null, record),
              }
              // {
              //   icon: 'ant-design:delete-outlined',
              //   color: 'error',
              //   popConfirm: {
              //     title: '是否确认删除',
              //     placement: 'left',
              //     confirm: handleDelete.bind(null, record),
              //   },
             // },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="register" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { userRewardsListApi,importUserRewardsExcelApi } from '@/api/sys/rewards';

  import FormModal from './FormModal.vue';
  import { ref } from 'vue';

  import { columns, searchFormSchema } from './role.data';
  import {useMessage} from "@/hooks/web/useMessage";
  import {useModal} from "@/components/Modal";
  import {aoaToSheetXlsx, ImpExcel} from '@/components/Excel';

  defineOptions({ name: 'userRewardsList' });
  const { createMessage } = useMessage();

  const [register, { openModal, closeModal }] = useModal();


  const loading=ref(false);

  const [registerTable, { reload }] = useTable({
    title: '用户奖惩记录列表',
    api: userRewardsListApi,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    // actionColumn: {
    //   width: 80,
    //   title: '操作',
    //   dataIndex: 'action',
    //   // slots: { customRender: 'action' },
    //   fixed: undefined,
    // },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    //console.log(record);

    deleteApi(record).then(res => {
      createMessage.success(res.message);
      })
      .catch((res) => {
        createMessage.error(res.message);
      })
      .finally(() => {
        reload();
      });
  }

  function handleSuccess() {
    reload();
  }


  const arrHeader=[
    {
      key:"userName",
      value:"姓名"
    },
    {
      key:"note",
      value:"奖惩原因"
    },
    {
      key:"name",
      value:"奖惩名称"
    },

    {
      key:"year",
      value:"年份"
    }
  ];

  function getHeadArr(){
    let arr=[];

    arrHeader.forEach(r=>{
      arr.push(r.value)
    });
    return arr;
  }

  function changeSubmitArr(results){
    let data={
      list:[]
    }
    let list=[];
    // console.log("---arrHeader::",arrHeader)
    // console.log("---results:",results)
    results.forEach(r=>{
      let json={};
      for(let key in r){
        arrHeader.forEach(e=>{
          if(e.value==key){
            json[e.key]=r[key];
          }
        })
      }
      data.list.push(json)
    });
    return data;
  }

  function excelSuccessFn(list){
    let results=list[0].results;
    // console.log(results);
    let data=changeSubmitArr(results);
  //  console.log(data)
    loading.value=true;
    importUserRewardsExcelApi(data).then(res=>{
      loading.value=false;

      if(res.code==200){
        createMessage.success("导入成功!");
        reload();
      }else{
        createMessage.error(res.message);
      }

    })
  }
  function aoaToExcel() {
    // 保证data顺序与header一致
    let arrHeader=[ "姓名","奖惩原因","奖惩名称","年份"];
    aoaToSheetXlsx({
      data: [],
      header: arrHeader,
      filename: '人员奖惩模版.xlsx',
    });
  }



</script>
